import { baseURL } from '@/utils/http'

/**
     * 文件上传
     * @param {Object} path
     *
     */
export const uploadFilePromise = (path : string) => {
  return new Promise((resolve, reject) => {
    uni.showLoading({
      title: '上传中...',
      mask: true
    })
    uni.uploadFile({
      url: `${baseURL}/web/cl/attach/putfileAttach.json`, // 图片目前只有正式能用
      filePath: path,
      name: 'file',
      formData: {
        user: 'user'
      },
      header: {
        'Blade-Auth': 'bearer ' + uni.getStorageSync('accessToken')
      },
      success: (res : any) => {
        uni.hideLoading()
        console.log(res)
        console.log(res.data)
        if (res.statusCode === 200) {
          let data = JSON.parse(res.data)
          resolve({
            url: data.link,
            fileId: data.fileCode
          })
        } else {
          uni.showToast({
            title: '上传失败',
            icon: 'none'
          })
        }
      },
      fail: (err) => {
        uni.hideLoading()
        console.log(err)
      }
    });
  })
}

//获取文件类型
export const getFileType = (url : any) => {
  // console.log(url)
  return url.split('.').pop().toLowerCase().split(/\#|\?/)[0];
}
//获取文件名
export const getFileName = (url : any) => {
  const regex = /\/([^\/?]+)(\?|$)/;
  const match = url.match(regex);
  return match ? match[1] : '';
}
